package im.general;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import im.clusterhead.*;
import im.clusterhead.OurFormatter;
import im.eventservice.*;
import im.general.Config;
import im.node.MobileNodeAgent;
import im.resources.ResourceStatus;
import jade.core.behaviours.CyclicBehaviour;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.logging.SimpleFormatter;

/**
 *
 * @author Giovanni
 */
public class AgentLogBehaviour extends CyclicBehaviour {

    private static Logger log = Logger.getLogger("General Logger");
    private static FileHandler fh = null;
    private static OurFormatter sf = new OurFormatter();
    private BaseAgent ba;

    public AgentLogBehaviour(BaseAgent ba){
        this.ba = ba;

        if(fh!=null)
            return;

        try {
            fh = new FileHandler(Config.LOGGING_DIRECTORY + "//" + Config.GENERAL_LOGGING_FILE/*+ "."+ba.getLocalName()*/+".csv");//mod
        } catch (IOException ex) {
            Logger.getLogger(AgentLogBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(AgentLogBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
        fh.setFormatter(sf);
        log.addHandler(fh);
        log.setLevel(Level.ALL);
    }

   

    public String getTimeStampNow() {

        Calendar cal = new GregorianCalendar();
        Timestamp ts = new Timestamp(cal.getTimeInMillis());
        int giorno = cal.get(Calendar.DAY_OF_MONTH);
        int mese = cal.get(Calendar.MONTH);
        int anno = cal.get(Calendar.YEAR);
        int ora = cal.get(Calendar.HOUR);
        int minuti = cal.get(Calendar.MINUTE);
        int secondi = cal.get(Calendar.SECOND);
        String orario;

        if (cal.get(Calendar.AM_PM) == 0) {
            orario = "A.M.";
        } else {
            orario = "P.M.";
        }

        return ""+cal.getTimeInMillis();
        //return "" + giorno + "-" + (mese + 1) + "-" + anno + " " + ora + ":" + minuti + ":" + secondi + " " + orario + "";

    }

   

    @Override
    public void action() {
        //throw new UnsupportedOperationException("Not supported yet.");

            log();
            
            block(5000);

    }

    protected synchronized void log() {

        int messageCount = ba.readMessageCount();

        boolean
                logMessages=true,
                logRsrcCH=false,
                logRsrcMN=false;

//        if(messageCount!=0)
//            log.log(Level.ALL, "" + getTimeStampNow() +";"+myAgent.getLocalName()+ ";" +ba.getContainerName()+";"+messageCount+";"+"MSG");
        if(this.myAgent instanceof ClusterHeadAgent && ((ClusterHeadAgent)this.myAgent).getStatus() != null) {
            ResourceStatus status = ((ClusterHeadAgent)this.myAgent).getStatus();
            log.log(Level.ALL, "" + getTimeStampNow() +";"+myAgent.getLocalName()+ ";" +ba.getContainerName()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getFreeBWKbps()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getFreeCPUMhz()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getFreeDiskMB()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getFreeRamMB()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getChargeReminderMah()+";"+ ((ClusterHeadAgent)this.ba).getStatus().getStatusIndex()+";"+((ClusterHeadAgent)this.ba).getAttachedNodes()+";"+((ClusterHeadAgent)this.ba).getStatus().getStatusIndex()/((ClusterHeadAgent)this.ba).getAttachedNodes());
        }

        /*if(logRsrcMN && this.myAgent instanceof MobileNodeAgent)
        log.log(Level.ALL, "" + getTimeStampNow() +";"+myAgent.getLocalName()+ ";" +ba.getContainerName()+";"+ ((MobileNodeAgent)this.ba).getStatus().getFreeBWKbps()+";"+ ((MobileNodeAgent)this.ba).getStatus().getFreeCPUMhz()+";"+ ((MobileNodeAgent)this.ba).getStatus().getFreeDiskMB()+";"+ ((MobileNodeAgent)this.ba).getStatus().getFreeRamMB()+";"+ ((MobileNodeAgent)this.ba).getStatus().getChargeReminderMah()+";"+ ((MobileNodeAgent)this.ba).getStatus().getStatusIndex()+";"+"RSRC");*/
    }
}
